using UnityEngine;
using System.Collections;

public class Mouse : MonoBehaviour {
	
	private static float lastUpdate;
	private static float deltaTime = 0.010f; // update name frequency
	//private static string buttonName;
	private static GameObject button;
	
	private static float lastClick = -1;
	
	void Awake(){
		lastUpdate = 0;
		//buttonName = "";
		button = null;
	}
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	public static bool isDoubleClick(GameObject obj){
		bool b = false;
		if(Input.GetMouseButtonDown(0)){
			float delta = Time.time - lastClick;
			if(delta  < 0.37f && delta > 0.1f)
				b = isOver(obj);
			lastClick = Time.time;
		}
		return b;
	}
	
	public static bool isClick(GameObject obj){
		if(Input.GetMouseButtonDown(0))
			return isOver(obj);
		
		else return false;
	}
	
	public static bool isClick(){
		return  Input.GetMouseButton(0);
	}
	
	public static bool onClick(){
		return  Input.GetMouseButtonDown(0);
	}
	
	public static bool isRightHold(){
		return Input.GetMouseButton(1);
	}
	
	public static bool isHold(GameObject obj){
		if(Input.GetMouseButton(0))
			return isOver(obj);
		
		else return false;
	}
	
	public static bool isOnRigidbody(){
		if(isUpdate())
			updateButton();
		if(button == null)
			return false;
		return  true; //button.rigidbody != null;
	}
	
	public static bool isOnButton(){
		if(isUpdate())
			updateButton();
		if(button == null)
			return false;
		return button.tag == "Button";
	}
	
	
	
	public static bool isOver(GameObject obj){
		if(isUpdate())
			updateButton();
		return obj == button;
	}
	
	private static bool isUpdate(){
		return Time.time > lastUpdate + deltaTime;
	}
	
	private static void updateButton(){
		button = setButton();
		lastUpdate = Time.time;
	}
	
	private static GameObject setButton(){
		RaycastHit hit = new RaycastHit();
		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		
		if (!Physics.Raycast(ray,out  hit, Mathf.Infinity))
			return null;
		
		Debug.DrawLine(ray.origin, hit.point);
		
		if (!hit.collider)
			return null;
		
		return hit.collider.gameObject;
	}
	
	public static GameObject getButton(){
		return button;
	}
}
